package org.zhouhe.modules.imageSlider.service;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.zhouhe.base.BaseService;
import org.zhouhe.util.InitSystem;
import org.zhouhe.vo.ImageSlider;

@Repository
@SuppressWarnings("unchecked")
public class ImageSliderService extends BaseService {
	public List<ImageSlider> getImageSliderList(Integer count) {
		Query q = dao.getSessionFactory().getCurrentSession().createQuery("select distinct s from ImageSlider s order by s.orderId ASC, s.imageSliderId ASC");
		if (count != null) {
			q.setMaxResults(count);
		}
		return q.list();
	}

	public void save(ImageSlider imageSlider) {
		dao.save(imageSlider);
		InitSystem.reloadSliders();
	}

	public void delete(ImageSlider imageSlider) {
		if (imageSlider.getArticle() != null) {
			dao.executeUpdate("delete from ImageSlider where article.articleId=" + imageSlider.getArticle().getArticleId());
		} else if (imageSlider.getReport() != null) {
			dao.executeUpdate("delete from ImageSlider where report.reportId=" + imageSlider.getReport().getReportId());
		} else {
			dao.delete(imageSlider);
		}
		InitSystem.reloadSliders();
	}

	public void updateOrder(List<ImageSlider> imageSliderList) {
		for (ImageSlider is : imageSliderList) {
			dao.executeUpdate("update ImageSlider set orderId="+ is.getOrderId() +" where imageSliderId="+is.getImageSliderId());
		}
	}

}
